import os
import re
from typing import Iterable

from scrapy import Request
from scrapy.selector import Selector

from database.mongo_client import sync_client
from tutorial.items.fanqienovel_item import FanqienovelItem
from tutorial.spiders.curl_spiders import CurlSpider
from utils.label_font import LabelFont


class FanqienovelSpider(CurlSpider):
    name = "fanqienovel"

    # allowed_domains = ["fanqienovel.com"]
    # start_urls = ["https://fanqienovel.com"]

    def start_requests(self) -> Iterable[Request]:
        # yield Request(
        #     url="https://fanqienovel.com/page/7143038691944959011",
        #     # url="https://httpbin.org/get",
        #     callback=self.parse,
        #     meta={'book_name': "shi_ri_zhong_yan"}
        # )

        item = sync_client['ebooks']['shi_ri_zhong_yan'].find_one({"chapter_index": 746})
        yield Request(url=f"https://fanqienovel.com{item['url']}", callback=self.parse_chapter, cookies={
            # 'novel_web_id': '7417684517664671244',
            'novel_web_id': '7421862640937731622',
        })

    async def parse(self, response, **kwargs):
        titles: list[Selector] = response.css('.chapter-item-title')
        for title in titles:
            r = re.search("第(\d+)章", title.root.text or "")
            if r:
                yield FanqienovelItem(
                    book_name=response.meta['book_name'],
                    chapter_index=int(r.group(1)),
                    chapter_title=title.root.text,
                    url=title.attrib['href'],
                )

    @property
    def font_crack(self):
        if hasattr(self, '_font_crack') and self._font_crack:
            return self._font_crack
        json_path = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
            'static',
            'json',
            'fanqie.json'
        )
        self._font_crack = LabelFont.get_results_json(json_path)
        return self._font_crack

    def parse_chapter(self, response, **kwargs):
        def repl(match):
            return self.font_crack[match.group()]

        list_p: list[Selector] = response.css('.muye-reader-content p')
        for p in list_p:
            if p.root.text:
                p_text = re.sub('|'.join(self.font_crack.keys()), repl, p.root.text)
                print(p_text)
